//  我的-报名记录页面
const baseUrl='https://gsdse.cn/api/KJRecruit';
const db = wx.cloud.database()
const _ = db.command
Page({

  /**
   * 页面的初始数据
   */
  data: {
    _openid: '',
    //得到的personinfo
    personinfo: [],
    willing: [], //返回的willing对象
    departmentName: '网络研发部', //传过来的departmentName,
    userId: '', //传过来的userId
    index: '',
    groupNumber: 1234, //面试群/联系方式，
    isadmit: false //是否已经被录取，用于控制录取按钮的显示
  },

  setTimeout(){
    return new Promise((resolve,reject)=>{
      setTimeout(function(res){
        wx.hideLoading()
      },3000)
    })
  },
  
  //=============================

  //查看报名表
  async _checkApplication() {
    console.log(this.data.willing.fileUrl);
    wx.showLoading({
      title: '全力奔跑中',
      mask: true
    })
    //打开文件
    wx.downloadFile({
      url: this.data.willing.fileUrl,
      success (res) {
        // console.log(res.tempFilePath)
        wx.openDocument({
          showMenu:true,
          filePath: res.tempFilePath,
          fileType:'pdf',
          success: async function (res) {
            console.log('打开文档成功')
          },
          complete: async function (res) {
            // console.log(this.data)
            wx.hideLoading()
          },
        })
      }
    })
    //防止它打不开就又卡死
    await this.setTimeout()
  },

  //录取功能
  async _admit() {
    //判断录取部门是用户的第几志愿
    console.log(this.data.userId)
    const willingStatus = await this.selectUserWilling(this.data.userId)
    console.log(willingStatus)
    const res = await this.updateStatus(willingStatus)
    wx.showToast({
      title: '录取成功！',
    })
    //隐藏录取和不录取按钮
    this.setData({isadmit:true})
  },
  
  //不录取功能
  async noAdmit() {
    //判断录取部门是用户的第几志愿
    console.log(this.data.userId)
    const willingStatus = await this.selectUserWilling(this.data.userId)
    console.log(willingStatus)
    const res = await this.updateStatusAsNo(willingStatus)
    wx.showToast({
      title: '操作成功！',
    })
    //隐藏录取和不录取按钮
    this.setData({ isadmit:true})
  },
  //修改录取为不录取
  async updateStatusAsNo(willingStatus) {
    return new Promise((resolve, reject) => {
      if (this.data.willing.firstWilling === this.data.departmentName) {
        willingStatus.status.firstStatus = 0
      } else if (this.data.willing.secondWilling === this.data.departmentName) {
        willingStatus.status.secondStatus = 0
      }
      console.log(willingStatus.status)
      //修改录取情况
      if(willingStatus.status.firstStatus === 0){
        wx.request({
          url: baseUrl+'/willingPass',
          method: 'POST',
          data: {
            applyId: this.data.personinfo.userId,
            firstStatus: willingStatus.status.firstStatus,
          },
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: function (res) {
            console.log(res)
            resolve(res.data)
          }
        }) 
      }else if(willingStatus.status.secondStatus === 0){
        wx.request({
          url: baseUrl+'/willingPass',
          method: 'POST',
          data: {
            applyId: this.data.personinfo.userId,
            secondStatus: willingStatus.status.secondStatus,
          },
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: function (res) {
            console.log(res)
            resolve(res.data)
          }
        }) 
      }
    })
  },

  //修改录取情况
  async updateStatus(willingStatus) {
    return new Promise((resolve, reject) => {
      if (this.data.willing.firstWilling === this.data.departmentName) {
        willingStatus.status.firstStatus = 1
      } else if (this.data.willing.secondWilling === this.data.departmentName) {
        willingStatus.status.secondStatus = 1
      }
      console.log(willingStatus.status)
      //修改录取情况
      if(willingStatus.status.firstStatus === 1){
        wx.request({
          url: baseUrl+'/willingPass',
          method: 'POST',
          data: {
            applyId: this.data.personinfo.userId,
            firstStatus: willingStatus.status.firstStatus,
          },
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: function (res) {
            console.log(res)
            resolve(res.data)
          }
        }) 
      }else if(willingStatus.status.secondStatus === 1){
        wx.request({
          url: baseUrl+'/willingPass',
          method: 'POST',
          data: {
            applyId: this.data.personinfo.userId,
            secondStatus: willingStatus.status.secondStatus,
          },
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: function (res) {
            console.log(res)
            resolve(res.data)
          }
        }) 
      }
    })
  },

  //获取用户信息
  selectUserInfo(userId) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: baseUrl+'/selectUser',
        method: 'POST',
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        data: {
          userId: userId
        },
        success: function (res) {
          console.log(res.data.data)
          resolve({
            avatarUrl:res.data.data.avatarUrl,
            userName: res.data.data.userName,
            schoolClass: res.data.data.schoolClass
          })
        }
      })
    })
  },

  //查询用户志愿信息
  selectUserWilling(userId) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: baseUrl+'/SelectUserWilling',
        method: 'POST',
        data: {
          userId: userId
        },
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success: function (res) {
          let departmentArray = ['秘书处', '网络研发部', "影视部", '动画部', "PC技术部", "新媒体运营部", 'UI设计部']
          resolve({
            willing: {
              firstWilling: departmentArray[res.data.data.firstWilling - 1],
              secondWilling: departmentArray[res.data.data.secondWilling - 1],
              fileUrl: res.data.data.pdfUrl
            },
            status: {
              firstStatus: res.data.data.firstStatus,
              secondStatus: res.data.data.secondStatus
            }
          })
        }
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    // 接受传过来的openid和departmentName
    // console.log(options);
    this.setData({
      userId: options.userId,
      departmentName: options.departmentName
    })
    // 查询用户表拿到userInfo
    const userInfo = await this.selectUserInfo(options.userId)
    //拿志愿信息
    const userWilling = await this.selectUserWilling(options.userId)

    //判断是否已经录取过了
    let haveAdmit = false;

    if(
      this.data.departmentName===userWilling.willing.firstWilling &&
      (userWilling.status.firstStatus==1||userWilling.status.firstStatus==0)) haveAdmit=true;
    else if(
      this.data.departmentName===userWilling.willing.secondWilling &&
      (userWilling.status.secondStatus==1||userWilling.status.secondStatus==0))haveAdmit=true;
    console.log(haveAdmit)

    //传入data对象中
    this.setData({
      personinfo: {
        userId: options.userId,
        userName: userInfo.userName,
        schoolClass: userInfo.schoolClass,
        avatarUrl:userInfo.avatarUrl
      },
      willing: {
        firstWilling: userWilling.willing.firstWilling,
        secondWilling: userWilling.willing.secondWilling,
        fileUrl: userWilling.willing.fileUrl
      },
      
      isadmit:haveAdmit
      // isadmit:false
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})